﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace ElingOA6_DSJCJ.DAL
{
    /// <summary>
    /// 通用类
    /// </summary>
    public partial class Common
    {

        /// <summary>
        /// 分页获取数据
        /// <creator>wangl</creator>
        /// <date>2013-09-06</date>
        /// </summary>       
        /// <param name="tableName">表名</param>
        /// <param name="fields">字段名(全部字段为*)</param>
        /// <param name="pageSize">每页显示多少条记录</param>
        /// <param name="pageIndex">指定当前为第几页 </param>
        /// <param name="strWhere">条件语句(不用加where) </param>
        /// <param name="fOrderFiled">排序字段(必须!支持多字段)  </param>
        /// <param name="orderType">排序方式，非0则降序</param>
        /// <param name="total">返回总记录数</param>
        /// <returns>数据列表</returns>
        public DataSet GetListByPage(string tableName, string fields, int pageSize, int pageIndex, string strWhere, string fOrderFiled, int orderType, out int total)
        {
            SqlParameter[] parameters = {  
                    new SqlParameter("@tableName", SqlDbType.VarChar, 500),  
                    new SqlParameter("@fields", SqlDbType.VarChar, 2000),  
                    new SqlParameter("@orderField", SqlDbType.VarChar, 500),  
                    new SqlParameter("@sqlWhere", SqlDbType.VarChar,2000),  
                    new SqlParameter("@pageSize", SqlDbType.Int),  
                    new SqlParameter("@pageIndex", SqlDbType.Int),  
                    new SqlParameter("@orderType", SqlDbType.Int),  
                    new SqlParameter("@TotalPage", SqlDbType.Int),
                    new SqlParameter("@TotalRecordCount", SqlDbType.Int)  
                };

            parameters[0].Value = tableName;
            parameters[1].Value = fields;
            parameters[2].Value = fOrderFiled;
            parameters[3].Value = strWhere;
            parameters[4].Value = pageSize;
            parameters[5].Value = pageIndex;
            parameters[6].Value = orderType;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;
            DataSet ds = DbHelperSQL.RunProcedure("up_GetRecordByPage", parameters, "ds", 60 * 30);
            total = int.Parse(parameters[8].Value.ToString());
            return ds;
        }
        /// <summary>
        /// 获取数据库时间
        /// 如果数据库无法获取则取当前的本地时间
        /// <creator>wangl</creator>
        /// <date>2013-11-05</date>
        /// </summary>
        /// <returns></returns>
        public DateTime GetServerDateTime()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT GETDATE() ");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj != null)
            {
                return (DateTime)obj;
            }
            else
            {
                return DateTime.Now;
            }
        }
    }
}
